package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysDict;
import cn.wgx.modules.sys.services.SysDictService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

//todo 字典模块待完成
@Api(tags = "4. 数据字典",produces = "小数据")
@RestController
@RequestMapping(value="${allPathPre}sysdict")
public class SysDictController extends BaseController {

    @Resource
    SysDictService sysDictService;

    @ApiOperation(value = "列表",notes = "可过滤字段: type, key, value, parentKey")
    @RequestMapping(path = "list", method = RequestMethod.GET)
    @ApiPermission(false)
    public Map<String, Object> getList(@ModelAttribute SysDict record,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "1000") Integer pageSize) {
        return returnData(PageHelper.startPage(page,  Math.min(Math.abs(pageSize), maxPageSize)), sysDictService.getList(record,null,null));
    }

    @ApiOperation("根据类型获取列表")
    @RequestMapping(path = "get", method = RequestMethod.GET)
    @ApiPermission(false)
    public Map<String, Object> get(@RequestParam String type) {
        SysDict sysDict = new SysDict();
        sysDict.setType(type);
        return returnData(sysDictService.getList(sysDict,null,null));
    }

    @ApiOperation("获取类型(type)列表")
    @RequestMapping(path = "type", method = RequestMethod.GET)
    @ApiPermission(false)
    public Map<String, Object> getTypeList() {
        SysDict sysDict = new SysDict();
        return returnData(sysDictService.getTypeSet());
    }

    @ApiOperation("新增")
    @RequestMapping(path = "insert", method = RequestMethod.POST)
    @ApiPermission
    public Map<String, Object> insertOne(@RequestBody SysDict record) {
        if(record.getK() == null || record.getV() == null || record.getType() == null){
            return returnErrorMap("参数错误");
        }
        return returnData(sysDictService.insertOne(record));
    }


    @ApiOperation("更新")
    @RequestMapping(path = "update", method = RequestMethod.PATCH)
    @ApiPermission
    public Map<String, Object> updateOne(@RequestBody SysDict record) {
        return returnData(sysDictService.updateOne(record));

    }

    @ApiOperation("删除")
    @RequestMapping(path = "del/{id}", method = RequestMethod.DELETE)
    @ApiPermission
    public Map<String, Object> deleteOne(@PathVariable("id") Integer id) {
        return returnData(sysDictService.deleteOne(id));
    }

    @ApiOperation("刷新缓存")
    @RequestMapping(path = {"flush"}, method = RequestMethod.DELETE)
    @ApiPermission(false)
    public Map<String, Object> clearCache(@RequestParam(value = "type", required = false) String type) {
        return returnData(sysDictService.clearCache(type));
    }
}